package com.yunfeisoft.controller.business;

import com.applet.base.BaseController;
import com.applet.utils.Page;
import com.applet.utils.Response;
import com.applet.utils.ResponseUtils;
import com.applet.utils.Validator;
import com.yunfeisoft.business.model.PmsProductAttribute;
import com.yunfeisoft.business.service.inter.PmsProductAttributeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: PmsProductAttributeController
 * Description: 商品属性表Controller
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Controller
public class PmsProductAttributeController extends BaseController {

    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;

    /**
     * 添加商品参数
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductAttribute/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(PmsProductAttribute record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "productAttributeCategoryId", "所属类型为空");
        validator.required(request, "name", "名称为空");
        validator.required(request, "selectType", "是否可选为空");
        validator.required(request, "inputType", "录入方式为空");
        /*if (record.getInputType() != null && record.getInputType() == 1) {
            validator.required(request, "inputList", "可选值列表为空");
        }*/
        if (record.getSelectType() != null && record.getSelectType() != 0) {
            validator.required(request, "inputList", "可选值列表为空");
        }
        validator.required(request, "handAddStatus", "是否手动新增为空");
        validator.zIndex(request, "sort", "排序值不合法");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        if (StringUtils.isNotBlank(record.getInputList())) {
            record.setInputList(record.getInputList().replaceAll("，", ",").replaceAll("\n", ","));
        }
        record.setType(PmsProductAttribute.ProductAttributeTypeEnum.PARAM.getValue());
        pmsProductAttributeService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 编辑商品参数
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductAttribute/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(PmsProductAttribute record, HttpServletRequest request, HttpServletResponse response) {
        return modifyStand(record, request, response);
    }

    /**
     * 添加商品属性
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductAttribute/saveStandard", method = RequestMethod.POST)
    @ResponseBody
    public Response saveStandard(PmsProductAttribute record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "productAttributeCategoryId", "所属类型为空");
        validator.required(request, "name", "名称为空");
        validator.required(request, "selectType", "是否可选为空");
        validator.required(request, "inputType", "录入方式为空");
        /*if (record.getInputType() != null && record.getInputType() == 1) {
            validator.required(request, "inputList", "可选值列表为空");
        }*/
        if (record.getSelectType() != null && record.getSelectType() != 0) {
            validator.required(request, "inputList", "可选值列表为空");
        }
        validator.required(request, "handAddStatus", "是否手动新增为空");
        validator.zIndex(request, "sort", "排序值不合法");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        if (StringUtils.isNotBlank(record.getInputList())) {
            record.setInputList(record.getInputList().replaceAll("，", ",").replaceAll("\n", ","));
        }
        record.setType(PmsProductAttribute.ProductAttributeTypeEnum.STANDARD.getValue());
        pmsProductAttributeService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改商品属性
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductAttribute/modifyStand", method = RequestMethod.POST)
    @ResponseBody
    public Response modifyStand(PmsProductAttribute record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "productAttributeCategoryId", "所属类型为空");
        validator.required(request, "name", "名称为空");
        validator.required(request, "selectType", "是否可选为空");
        validator.required(request, "inputType", "录入方式为空");
        /*if (record.getInputType() != null && record.getInputType() == 1) {
            validator.required(request, "inputList", "可选值列表为空");
        }*/
        if (record.getSelectType() != null && record.getSelectType() != 0) {
            validator.required(request, "inputList", "可选值列表为空");
        }
        //validator.required(request, "handAddStatus", "是否手动新增为空");
        validator.zIndex(request, "sort", "排序值不合法");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        if (StringUtils.isNotBlank(record.getInputList())) {
            record.setInputList(record.getInputList().replaceAll("，", ",").replaceAll("\n", ","));
        }
        pmsProductAttributeService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询商品属性表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductAttribute/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        PmsProductAttribute record = pmsProductAttributeService.load(id);
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询商品参数
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductAttribute/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String categoryId = ServletRequestUtils.getStringParameter(request, "categoryId", null);
        String name = ServletRequestUtils.getStringParameter(request, "name", null);

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("name", name);
        params.put("categoryId", categoryId);
        params.put("type", PmsProductAttribute.ProductAttributeTypeEnum.PARAM.getValue());

        Page<PmsProductAttribute> page = pmsProductAttributeService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 分页查询商品属性
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductAttribute/standardList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response standardList(HttpServletRequest request, HttpServletResponse response) {
        String categoryId = ServletRequestUtils.getStringParameter(request, "categoryId", null);
        String name = ServletRequestUtils.getStringParameter(request, "name", null);

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("name", name);
        params.put("categoryId", categoryId);
        params.put("type", PmsProductAttribute.ProductAttributeTypeEnum.STANDARD.getValue());

        Page<PmsProductAttribute> page = pmsProductAttributeService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 查询商品属性
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductAttribute/queryList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response queryList(HttpServletRequest request, HttpServletResponse response) {
        String categoryId = ServletRequestUtils.getStringParameter(request, "categoryId", null);
        String name = ServletRequestUtils.getStringParameter(request, "name", null);

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("name", name);
        params.put("categoryId", categoryId);
        //params.put("type", PmsProductAttribute.ProductAttributeTypeEnum.STANDARD.getValue());

        List<PmsProductAttribute> list = pmsProductAttributeService.queryList(params);
        return ResponseUtils.success(list);
    }

    /**
     * 批量删除商品属性表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductAttribute/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsProductAttributeService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }

    /**
     * 添加商品属性可选值列表选项
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsProductAttribute/addValue", method = RequestMethod.POST)
    @ResponseBody
    public Response addValue(HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "value", "属性值为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        String value = ServletRequestUtils.getStringParameter(request, "value", null);
        pmsProductAttributeService.addValue(id, value);
        return ResponseUtils.success("保存成功");
    }
}
